import random



def dict_sample(
    datas: dict, 
    n: int=20,
    return_rest: bool=True,
    ) -> dict:
    """
        dict每一个key作为一条数据进行采样
    """
    keys = [key for key in datas.keys()]
    if len(keys) > n:
        sample_keys = random.sample(keys, n)
    else:
        sample_keys = keys
    
    sample_datas = {}
    for key in sample_keys:
        sample_datas[key] = datas[key]
        del datas[key]
        
    if return_rest:
        return sample_datas, datas
    else:
        return sample_datas


def list_sample(
    datas: list, 
    n: int=20,
    return_rest: bool=True
    ) -> list:
    """
        list中随机选择n个元素
    """
    if len(datas) > n:
        sample_index = random.sample(range(0,len(datas)), n)
    else:
        sample_index = range(0, len(datas))
    sample_datas = []
    res_datas = []
    for index in range(0, len(datas)):
        if index in sample_index:
            sample_datas.append(datas[index])
        else:
            res_datas.append(datas[index])
            
    if return_rest:
        return sample_datas, res_datas 
    else:
        return sample_datas
    